package com.xl.flinkdemo.多流转换算子;

import com.xl.flinkdemo.entity.SensorReading;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.collector.selector.OutputSelector;
import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.SplitStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

import java.util.Collections;

/**
 * split：分流操作
 */
public class Transform_MultipleStreams {
    public static void main(String[] args) throws Exception {
        // 创建执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // 从文件中读取数据
        String inputPath = "D:\\myWork\\flinkdemo\\src\\main\\resources\\sensors";

        //得到数据源
        DataStream<String> inputdataStream = env.readTextFile(inputPath);

        DataStream<SensorReading> dataStream =
                inputdataStream.map(new MapFunction<String, SensorReading>() {
                    @Override
                    public SensorReading map(String s) throws Exception {
                        String[] fields = s.split(",");
                        return new SensorReading(fields[0], new Long(fields[1]), new Double(fields[2]));
                    }
                });


        // 1.分流 按照温度值的高低分为高温流和低温流，以30°为界

        SplitStream<SensorReading> split = dataStream.split(new OutputSelector<SensorReading>() {
            @Override
            public Iterable<String> select(SensorReading sensorReading) {
                return sensorReading.getSensors() >= 30 ? Collections.singletonList("high") : Collections.singletonList("low");
            }
        });

        //输出打印
        DataStream<SensorReading> highTemper = split.select("high");
        DataStream<SensorReading> lowTemper = split.select("low");
        DataStream<SensorReading> allTemper = split.select("high","low");
        highTemper.print("high");
        lowTemper.print("low");
        allTemper.print("all");


        // 合成流操作 connect,将高温流转换成二元组类型，与低温流连接合并之后，输出状态信息，低温：正常，高温：报警状态
        DataStream<Tuple2<String, Double>> warningStream = highTemper.map(new MapFunction<SensorReading, Tuple2<String, Double>>() {

            @Override
            public Tuple2<String, Double> map(SensorReading sensorReading) throws Exception {
                return new Tuple2<>(sensorReading.getId(), sensorReading.getSensors());
            }
        });

        ConnectedStreams<Tuple2<String, Double>, SensorReading> connectStream = warningStream.connect(lowTemper);
        DataStream<Object> resultStream = connectStream.map(new CoMapFunction<Tuple2<String, Double>, SensorReading, Object>() {

            @Override
            public Object map1(Tuple2<String, Double> value) throws Exception {
                return new Tuple3<>(value.f0, value.f1, "high temp warning");
            }

            @Override
            public Object map2(SensorReading value) throws Exception {
                return new Tuple2<>(value.getId(), "normal");
            }
        });
        resultStream.print();



        //union连接多条流
        //不成立：数据类型不一致 warningStream.union(lowTemper);

        env.execute();
    }
}
